﻿using System;
using EPiServer.Core;
using EPiServer.Core.Html;
using EPiServer.Web;
using TemplateFoundation.PageTypes;
using TemplateFoundation.Tags;

namespace TemplateFoundation.Tests.Environment
{
    public class RandomValuesPageData : MockPageData, IStandardPage
    {
        public RandomValuesPageData()
        {
            // Generate a number used as the page ID
            var pageId = Helpers.Instance.GetPageId();
            
            // Set a random page name
            PageName = string.Format("EPiServer page {0}", pageId);

            URLSegment = UrlSegment.CreateUrlSegment(this);

            // Set the PageLink to a PageReference with the ID generated
            PageLink = new PageReference(pageId);

            // Set language (every other page is in english)
            LanguageID = GetRandomBool() ? "sv" : "en";

            // Set master language (every other page is in english)
            MasterLanguageBranch = GetRandomBool() ? "sv" : "en";

            // Set dates and publish status (20% of pages should be published ahead in time))
            if(new Random().Next(1,10)<9)
            {
                StartPublish = new DateTime(
                    DateTime.Now.Year+1,
                    GetRandom(1, 12),
                    GetRandom(1, 25));

                // Set Status property
                Status=VersionStatus.Published;
            }
            else
            {
                StartPublish = new DateTime(
                    GetRandom(1980, DateTime.Now.Year-1),
                    GetRandom(1, 12),
                    GetRandom(1, 25));

                // Set Status property
                Status = VersionStatus.DelayedPublish;
            }

            Created = DateTime.Now.AddDays(-1*GetRandom(1, 700));
            CreatedBy = ContentPersonNames[GetRandom(0, ContentPersonNames.Length - 1)];

            // Set page type
            PageTypeID = 1;
            PageTypeName = "Random values page";

            // Set link
            LinkURL = "/Templates/Pages/RandomValuesPage.aspx?id=" + pageId;
            
            // Create tags
            for (int i=0; i<GetRandom(0, 5); i++)
            {
                Tags.Add(new Tag("Tag " + i+1, true));
            }

            // Set "MainBody" property
            string mainBodyContent = string.Empty;
            for (int i=0; i<GetRandom(1, ContentTextParagraphs.Length); i++)
            {
                mainBodyContent += string.Format("<h1>Paragraph {0}</h1>", i);
                mainBodyContent += string.Format("<p>{0}</p>", ContentTextParagraphs[i]);
            }

            MainBody = mainBodyContent;

            // Set "Author" property
            Author = ContentPersonNames[GetRandom(0, ContentPersonNames.Length-1)];
            
            // Set description
            Description = TextIndexer.StripHtml(mainBodyContent, 160);
        }

        #region IStandardPageType Members

        public string MainBody
        {
            get
            {
                return GetValue("MainBody") as string;
            }
            set
            {
                if(Property["MainBody"]==null)
                {
                    AddStringProperty("MainBody",string.Empty,true);
                }

                SetValue("MainBody", value);
            }
        }

        #endregion

        /// <summary>
        /// Gets a random number in the interval
        /// </summary>
        protected static int GetRandom(int min, int max)
        {
            return new Random().Next(min, max);
        }

        protected static bool GetRandomBool()
        {
            return GetRandom(0,int.MaxValue) % 2 == 0;
        }
    }
}
